<html>
<head><meta charset="utf-8"><title>Is unwinding through extern C UB? · project-ffi-unwind · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/index.html">project-ffi-unwind</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html">Is unwinding through extern C UB?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230112325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230112325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230112325">(Mar 12 2021 at 22:49)</a>:</h4>
<p>I read the RFC again and I'm not sure I understand exactly,<br>
Is this UB or converted to abort?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="k">fn</span> <span class="nf">call_this_from_c</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"message"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>(specifically in my case it's rust-&gt;c-&gt;rust and the last/inner rust is panicking).<br>
I thought it was abort but now that <a href="https://github.com/rust-lang/rust/pull/76570">https://github.com/rust-lang/rust/pull/76570</a> is merged we're getting sefaults: <a href="https://github.com/rust-bitcoin/rust-secp256k1/pull/288">https://github.com/rust-bitcoin/rust-secp256k1/pull/288</a><br>
(<code>(signal: 4, SIGILL: illegal instruction)</code>)</p>



<a name="230112907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230112907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230112907">(Mar 12 2021 at 22:56)</a>:</h4>
<p><span class="user-mention" data-user-id="327458">@katelyn martin</span> is this part of the behavior change behind the feature flag?</p>



<a name="230113319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230113319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230113319">(Mar 12 2021 at 23:00)</a>:</h4>
<p><code>SIGILL</code> is not a segfault, though; I believe it's considered a "valid" means of causing an <code>abort</code>. I think I remember <code>SIGILL</code> intentionally being our means of aborting-on-<code>panic</code>, but I can't remember where I heard this.</p>



<a name="230113609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230113609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230113609">(Mar 12 2021 at 23:03)</a>:</h4>
<p>This is crate documentation, not <code>rustc</code> documentation, but it seems to confirm that: <a href="https://docs.rs/panic-abort/0.3.2/panic_abort/">https://docs.rs/panic-abort/0.3.2/panic_abort/</a></p>
<blockquote>
<p>On hosted applications (applications running under an OS), the trap instruction usually terminates the whole process with an exit code that corresponds to SIGILL unless a signal handler that handles this particular signal was registered...</p>
</blockquote>



<a name="230113789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230113789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230113789">(Mar 12 2021 at 23:05)</a>:</h4>
<p>Ah, okay, here's something a little more definite: <a href="https://github.com/rust-lang/rust/issues/52633">https://github.com/rust-lang/rust/issues/52633</a></p>



<a name="230113965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230113965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230113965">(Mar 12 2021 at 23:07)</a>:</h4>
<p><code>rustc</code> lowers <code>abort</code> to <code>llvm.trap</code>, an intrinsic that is platform-specific: <a href="https://llvm.org/docs/LangRef.html#llvm-trap-intrinsic">https://llvm.org/docs/LangRef.html#llvm-trap-intrinsic</a></p>



<a name="230113998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230113998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230113998">(Mar 12 2021 at 23:07)</a>:</h4>
<p>So, yes, what you're seeing is an abort.</p>



<a name="230114351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114351">(Mar 12 2021 at 23:11)</a>:</h4>
<p>Oh that's a weird abort but cool.<br>
(why doest rust call libc::abort or prints before aborting here?)</p>
<p>So is this an intentional change even though c-unwind is feature gated?</p>



<a name="230114655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114655">(Mar 12 2021 at 23:15)</a>:</h4>
<p>Printing when you haven't asked for printing would be...irregular, in a low-level language <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="230114738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114738">(Mar 12 2021 at 23:16)</a>:</h4>
<p>I was in no way involved w/ implementing <code>rustc</code>, but I would guess the reason for not using <code>libc</code> is that it's not available on all platforms; deferring to the backend (LLVM) for an intrinsic seems more consistent.</p>



<a name="230114760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114760">(Mar 12 2021 at 23:16)</a>:</h4>
<p>(Also note that <code>abort</code> should only happen in exceptional cases; it's not a "happy path" way to halt a program.)</p>



<a name="230114795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114795">(Mar 12 2021 at 23:16)</a>:</h4>
<p>And, yes, this is an intentional change (it meets the RFC spec).</p>



<a name="230114841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114841">(Mar 12 2021 at 23:17)</a>:</h4>
<p>Looking over the code change itself, the <code>abort</code> behavior is untouched; I expect you would get the same exit code for any <code>panic</code> if you compiled with <code>panic=abort</code>.</p>



<a name="230114924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114924">(Mar 12 2021 at 23:18)</a>:</h4>
<p>If you want the old behavior from <code>extern "C"</code>, you should definitely opt into the feature gate.</p>



<a name="230114947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230114947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230114947">(Mar 12 2021 at 23:18)</a>:</h4>
<p>(or not use nightly)</p>



<a name="230115247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230115247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230115247">(Mar 12 2021 at 23:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120076">BatmanAoD (Kyle Strand)</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F/near/230114655">said</a>:</p>
<blockquote>
<p>Printing when you haven't asked for printing would be...irregular, in a low-level language <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>
</blockquote>
<p>Doesn't C's abort print? maybe I'm misremembering</p>



<a name="230115568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230115568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230115568">(Mar 12 2021 at 23:26)</a>:</h4>
<p><code>libc</code>? It may.</p>



<a name="230115579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230115579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230115579">(Mar 12 2021 at 23:26)</a>:</h4>
<p>I'm not sure either.</p>



<a name="230115999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230115999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230115999">(Mar 12 2021 at 23:31)</a>:</h4>
<p>It may also depend on the implementation of <code>libc</code>.</p>



<a name="230116883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230116883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230116883">(Mar 12 2021 at 23:44)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/47932">https://github.com/rust-lang/rust/issues/47932</a> related I guess.</p>



<a name="230116945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230116945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230116945">(Mar 12 2021 at 23:44)</a>:</h4>
<p>also less relatedly <a href="https://github.com/rust-lang/rust/issues/81895">https://github.com/rust-lang/rust/issues/81895</a></p>



<a name="230772070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230772070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> katelyn martin <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230772070">(Mar 17 2021 at 21:44)</a>:</h4>
<p>Hello! Apologies for the delay. To answer the question posed at the beginning of the thread, an unwinding operation crossing an <code>extern "C"</code> boundary prior to this RFC was considered UB. Today, that should now <code>abort</code>.</p>
<blockquote>
<p>is this part of the behavior change behind the feature flag?</p>
</blockquote>
<p><span class="user-mention" data-user-id="120076">@BatmanAoD (Kyle Strand)</span> Alas. I don't believe that is the case. Because that behavior was previously undefined however, would a patch to preserve that be worthwhile? I'm _inclined_ to guess not, but I have a bit less experience with the day-to-day of <code>rustc</code> maintenance.</p>



<a name="230780365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230780365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230780365">(Mar 17 2021 at 22:30)</a>:</h4>
<p>I think the old behavior should be preserved for as long as <code>extern "C-unwind"</code> is unstable.</p>



<a name="230787338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Is%20unwinding%20through%20extern%20C%20UB%3F/near/230787338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Is.20unwinding.20through.20extern.20C.20UB.3F.html#230787338">(Mar 17 2021 at 23:42)</a>:</h4>
<p>I think I agree with that. I was hoping it wouldn't make a difference because we could stabilize near-immediately, but I think stabilization must be blocked by the issue brought up in the "UB testcase" discussion.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>